package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.util.RedisUtil;
import com.cash.loan.modular.cash.result.ServerResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-09-05 9:43
 */
@Controller
@RequestMapping(value = "/cash/message")
public class MessageController {

    private static final String PREFIX = "/cash/message";

    /**
     * 短信平台选择缓存名
     */
    private static final String MESSAGE_PLATFORM = "message:platform";

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping(value = "/page")
    public String messagePage() {
        return PREFIX + "/message.html";
    }

    @ApiOperation("获得当前短信平台信息")
    @GetMapping(value = "/info")
    @ResponseBody
    public ServerResponse getMessagePlatform() {
        Map<String,Integer> result = new HashMap<>();
        if(!redisUtil.existKey(MESSAGE_PLATFORM)) {
            redisUtil.set(MESSAGE_PLATFORM,0+"");
            result.put("msgCode",0);
        } else {
            String msgResult = (String) redisUtil.get(MESSAGE_PLATFORM);
            Integer msgIntRes = Integer.valueOf(msgResult);
            result.put("msgCode",msgIntRes);
        }
        return ServerResponse.getSuccess("success",result);
    }

    /**
     * 设置短信平台
     * @param msgCode
     * @return
     */
    @ApiOperation("设置短信平台")
    @PostMapping(value = "/set")
    @ResponseBody
    public ServerResponse setMessagePlatform(@RequestParam(value = "msgCode") Integer msgCode) {
        if(msgCode != 0 && msgCode != 1) {
            return ServerResponse.getErrorMessage("平台编码错误");
        }
        redisUtil.set(MESSAGE_PLATFORM,msgCode+"");
        return ServerResponse.getSuccess("设置成功");
    }
}
